package com.glowworms.core.util;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.bee.tl.core.BeeException;
import org.bee.tl.core.GroupTemplate;
import org.bee.tl.core.Template;

import com.glowworms.core.constant.WebConstant;
import com.glowworms.core.web.listener.ContextHolder;

/**
 * 模板帮助类
 */
public abstract class TemplateUtils {
	
	/**
	 * 获取String模板
	 * @param src
	 * @param templateDataMap
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static String getString(String src, Map templateDataMap){
		TemplateConfiguration bean = ContextHolder.getApplicationContext().getBean(TemplateConfiguration.class);
		GroupTemplate group = bean.getGroupTemplate();
		Template t;
		try {
			t = group.getStringTemplate(src, WebConstant.ENCODE);
			Iterator<Entry<String, Object>> it = templateDataMap.entrySet().iterator();
			Entry<String, Object> entry = null;
			while (it.hasNext()) {
				entry = it.next();
				t.set(entry.getKey(), entry.getValue());
			}
			return t.getTextAsString();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (BeeException e) {
			e.printStackTrace();
		}
		return null;
	}
}
